<?php

namespace app\admin\controller\ykjp\inventory;

use app\common\controller\Backend;
use think\Db;

/**
 * 商品库存
 *
 * @icon fa fa-circle-o
 */
class Wp extends Backend {

    /**
     * Wp模型对象
     * @var \app\admin\model\ykjp\inventory\Wp
     */
    protected $model = null;

    // protected $distinguish=true;
    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\inventory\Wp;
    }

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::name('ykjp_wp_relationship')
                    ->alias('w')
                    ->join('ykjp_product p', 'w.product_id = p.id')
                    ->where($where)
                    ->where("w.firmid", $this->auth->firmid)
                    ->order('w.id', $order)
                    ->count();
            $list = Db::name('ykjp_wp_relationship')
                    ->alias('w')
                    ->field('w.*,p.name,p.unit,p.sku,p.prop,p.specification')
                    ->join('ykjp_product p', 'w.product_id = p.id')
                    ->where($where)
                    ->where("w.firmid", $this->auth->firmid)
                    ->order('w.id', $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();

            // 根据字段中储存的id，获取实际的值
            for ($i = 0; $i < count($list); $i++) {

                $w_id = $list[$i]['warehouse_id'];  //所在仓库id
                $p_id = $list[$i]['partition_id'];  //所在库区id

                $prop_list = json_decode($list[$i]['prop'], true);  //获取商品属性列表
                $prop = '';
                if (isset($prop_list)) {
                    // 解析商品属性
                    for ($j = 0; $j < count($prop_list); $j++) {
                        if (isset($prop_list[$j]['title'])) {
                            $title = $prop_list[$j]['title'];
                        } else {
                            $title = '';
                        }
                        if (isset($prop_list[$j]['value'])) {
                            $value = $prop_list[$j]['value'];
                        } else {
                            $value = '';
                        }
                        $prop .= $title . ':' . $value . ';';
                    }
                }

                $list[$i]['prop'] = $prop;  //商品属性
                //获取所在仓库名称
                $warehouse = Db::name('ykjp_warehouse')
                        ->field('name')
                        ->where("firmid", $this->auth->firmid)
                        ->where('id', $w_id)
                        ->find();
                $list[$i]['w_name'] = $warehouse['name'];

                //获取所在库区名称
                $partition = Db::name('ykjp_partition')
                        ->field('name')
                        ->where("firmid", $this->auth->firmid)
                        ->where('id', $p_id)
                        ->find();
                $list[$i]['p_name'] = $partition['name'];
            }
            //获取仓库
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 联动获取仓库
     */
    public function getWP() {
        //参考开发示例中的自定义搜索
        $type = $this->request->get('type');
        $partition_id = $this->request->get('partition_id');
        $list = null;
        if ($partition_id !== '') {
            if ($type == 'whouse') {
                //获取仓库列表
                $list = Db::name('ykjp_warehouse')
                        ->field('id as value,name')
                        ->where("firmid", $this->auth->firmid)
                        ->select();
            } else {
                //获取库区列表
                $list = Db::name('ykjp_partition')
                        ->field('id as value,name')
                        ->where("firmid", $this->auth->firmid)
                        ->where('warehouse_id', $partition_id)
                        ->select();
            }
        }
        $this->success('', null, $list);
    }

}
